package vip.fastgo.retroid.android.gamebox.view.widget

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.Button
import androidx.constraintlayout.widget.ConstraintLayout

/**
 * 上下左右，方向操作View
 */
@SuppressLint("ClickableViewAccessibility")
class DirectionView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : ConstraintLayout(context, attrs, defStyleAttr) {

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context): this(context, null)

    private val leftBtn = Button(context)
    private val upBtn = Button(context)
    private val rightBtn = Button(context)
    private val downBtn = Button(context)
    var onPressListener: OnPressListener? = null

    init {
        addLeftBtn()
        addUpBtn()
        addRightBtn()
        addDownBtn()
    }

    private fun addLeftBtn() {
        val layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        layoutParams.leftToLeft = LayoutParams.PARENT_ID
        layoutParams.topToTop = LayoutParams.PARENT_ID
        layoutParams.bottomToBottom = LayoutParams.PARENT_ID
        leftBtn.layoutParams = layoutParams
        leftBtn.text = "左"
        setTouchListener(leftBtn, Direction.LEFT)
        addView(leftBtn)
    }

    private fun addUpBtn() {
        val layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        layoutParams.leftToLeft = LayoutParams.PARENT_ID
        layoutParams.rightToRight = LayoutParams.PARENT_ID
        layoutParams.topToTop = LayoutParams.PARENT_ID
        upBtn.layoutParams = layoutParams
        upBtn.text = "上"
        setTouchListener(upBtn, Direction.UP)
        addView(upBtn)
    }

    private fun addRightBtn() {
        val layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        layoutParams.rightToRight = LayoutParams.PARENT_ID
        layoutParams.topToTop = LayoutParams.PARENT_ID
        layoutParams.bottomToBottom = LayoutParams.PARENT_ID
        rightBtn.layoutParams = layoutParams
        rightBtn.text = "右"
        setTouchListener(rightBtn, Direction.RIGHT)
        addView(rightBtn)
    }

    private fun addDownBtn() {
        val layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        layoutParams.leftToLeft = LayoutParams.PARENT_ID
        layoutParams.rightToRight = LayoutParams.PARENT_ID
        layoutParams.bottomToBottom = LayoutParams.PARENT_ID
        downBtn.layoutParams = layoutParams
        downBtn.text = "下"
        setTouchListener(downBtn, Direction.DOWN)
        addView(downBtn)
    }

    /**
     * 设置按钮的触摸事件
     *
     * @param view
     * @param direction 方向
     */
    private fun setTouchListener(view: View, direction: Direction) {
        view.setOnTouchListener { v, event ->
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    onPressListener?.onPress(v, direction)
                }
                MotionEvent.ACTION_UP -> {
                    onPressListener?.onLift(v, direction)
                }
                else -> {}
            }
            true
        }
    }

    interface OnPressListener {
        fun onPress(view: View, direction: Direction)

        fun onLift(view: View, direction: Direction)
    }

    enum class Direction {
        LEFT,UP,RIGHT,DOWN
    }
}